package com.newsystem.service.impl;

import com.newsystem.MyUtils.CommonResult;
import com.newsystem.MyUtils.MyUUIDUtils;
import com.newsystem.mapper.NewsMapper;
import com.newsystem.pojo.Collection;
import com.newsystem.mapper.CollectionMapper;
import com.newsystem.pojo.News;
import com.newsystem.service.ICollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenlitao
 * @since 2022-11-06
 */
@Service
public class CollectionService extends ServiceImpl<CollectionMapper, Collection> implements ICollectionService {

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private CollectionMapper collectionMapper;

    //用户收藏新闻或者取消收藏
    public CommonResult<Object> insertCollection(String userId, String newsId){
        News news = newsMapper.selectById(newsId);
        if(news!=null){
            Collection collection = collectionMapper.selectCollection(userId,newsId);
            if(collection!=null){
                if(news.getCollectionNum()!=null && news.getCollectionNum() > 0){
                    news.setCollectionNum(news.getCollectionNum()-1);
                }
                else {
                    news.setCollectionNum(0);
                }
                if(collectionMapper.deleteById(collection.getId())>0){
                    newsMapper.updateById(news);
                }
                return CommonResult.failed("取消收藏");
            }
            else {
                if(news.getCollectionNum()!=null){
                    news.setCollectionNum(news.getCollectionNum()+1);
                }
                else {
                    news.setCollectionNum(1);
                }
                collection = new Collection();
                collection.setId(MyUUIDUtils.getUUID());
                collection.setUserId(userId);
                collection.setNewsId(newsId);
                if(collectionMapper.insert(collection) > 0){
                    newsMapper.updateById(news);
                }
                return CommonResult.success("收藏操作成功");
            }
        }
        return CommonResult.failed("查无此新闻");
    }

    //用户通过收藏id取消收藏的新闻
    public CommonResult<Object> deleteCollection(String userId,String collentcionId){
        Collection collection = collectionMapper.selectByUserCollection(userId,collentcionId);
        if(collection == null){
            return CommonResult.failed("无此收藏");
        }
        News news = newsMapper.selectById(collection.getNewsId());
        if(news != null){
            if(news.getCollectionNum()!=null && news.getCollectionNum()>0){
                news.setCollectionNum(news.getCollectionNum()-1);
            }
            else {
                news.setCollectionNum(0);
            }
            newsMapper.updateById(news);
        }
        if(collectionMapper.deleteById(collentcionId)>0){
            return CommonResult.success("成功取消");
        }
        return CommonResult.failed("取消失败");
    }
}
